/********************************************************************************
 * Project Name		[JavaEE_Web]
 * File Name     	[URLRedirectServlet.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package session;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
@WebServlet(name = "URLRedirectServlet", urlPatterns = "/urlRedirect")
public class URLRedirectServlet extends HttpServlet {

	private static final long serialVersionUID = -1151481501791334179L;

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if (method == null || method.equals("")) {
			this.listBook(request, response);
		} else if ("buyBook".equals(method)) {
			this.buyBook(request, response);
		} else if ("shoppingCart".equals(method)) {
			this.showShoppingCart(request, response);
		}
	}

	/**
	 * 列出所有的书
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void listBook(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		request.getSession();
		out.write("本网站有如下书: <br/>");
		Set<Map.Entry<String, Book>> books = Database.getDatabase().entrySet();
		for (Map.Entry<String, Book> entry : books) {
			Book book = entry.getValue();
			String url = request.getContextPath() + "/urlRedirect?method=buyBook&id=" + book.getId();
			// response.encodeURL(String url)对表单action和超链接的url地址进行重写
			url = response.encodeURL(url);
			out.println(book.getName() + " <a href='" + url + "'>购买</a><br/>");
		}
	}

	/**
	 * 购买图书<br/>
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void buyBook(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		Book book = Database.getDatabase().get(id);
		HttpSession session = request.getSession();

		// 从Session中获得要买的书的集合
		@SuppressWarnings("unchecked")
		List<Book> list = (List<Book>) session.getAttribute("booklist");
		if (list == null) {
			list = new ArrayList<Book>();
			session.setAttribute("booklist", list);
		}
		list.add(book);
		// response.encodeRedirectURL(String url)用于对sendRedirect方法后的url地址进行重写
		String url = response.encodeRedirectURL(request.getContextPath() + "/urlRedirect?method=shoppingCart");
		response.sendRedirect(url);
	}

	/**
	 * 显示购买后的结果
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void showShoppingCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		@SuppressWarnings("unchecked")
		List<Book> list = (List<Book>) session.getAttribute("booklist");
		if (list == null || list.size() == 0) {
			out.write("对不起, 您还没有购买任何商品!!");
			return;
		}

		out.write("您买过如下商品:<br>");
		for (Book book : list) {
			out.write(book.getName() + "<br/>");
		}
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class Database {

	private static Map<String, Book> database = new LinkedHashMap<String, Book>();

	static {
		database.put("1", new Book("1", "book1"));
		database.put("2", new Book("2", "book2"));
		database.put("3", new Book("3", "book3"));
		database.put("4", new Book("4", "book4"));
		database.put("5", new Book("5", "book5"));
	}

	public static Map<String, Book> getDatabase() {
		return database;
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class Book {

	private String id;
	private String name;

	public Book() {
		super();
	}

	public Book(String id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
